package com.hw.paltfrom.service.system;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hw.paltfrom.mapper.HwBaseRoleMapper;
import com.hw.paltfrom.mapper.HwBaseUserMapper;
import com.hw.paltfrom.mapper.HwBaseUserRelationMapper;
import com.hw.paltfrom.pojo.HwBaseRole;
import com.hw.paltfrom.pojo.HwBaseUser;
import com.hw.paltfrom.pojo.HwBaseUserRelation;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.BaseUserRelationListVo;
import com.hw.paltfrom.vo.BaseUserRelationVo;

@Service
public class BaseUserRelationServer {
	@Autowired
	private HwBaseUserRelationMapper baseUserRelationMapper;
	
	@Autowired
	private HwBaseRoleMapper hwBaseRoleMapper;
	
	@Autowired
	private HwBaseUserMapper hwBaseUserMapper;

	public Map<String, Object> insert(List<BaseUserRelationVo> list, String roleid) {

		//// 查看此用户集合是否存在 与该角色已经绑定 的用户
		// List<BaseUserRelationVo> insertList=new
		//// ArrayList<BaseUserRelationVo>();
		// for(BaseUserRelationVo vo: list){
		// Wrapper<HwBaseUserRelation> wrapper = new
		//// EntityWrapper<HwBaseUserRelation>();
		// wrapper.eq("F_UserId", vo.getfUserid()).and().eq("F_RoleId",
		//// vo.getfRoleid());
		// List<HwBaseUserRelation> selectList =
		//// baseUserRelationMapper.selectList(wrapper);
		//// 未绑定关系的保存至集合
		// if(selectList.isEmpty()){
		// insertList.add(vo);
		// }
		// }

		// 批量插入数据
		// for(BaseUserRelationVo vo: insertList){

		// 删除当前角色的所有用户
		Map<String, Object> columnMap = new HashMap<>();
		columnMap.put("F_RoleId", roleid);
		baseUserRelationMapper.deleteByMap(columnMap);

		for (BaseUserRelationVo vo : list) {
			HwBaseUserRelation baseUserRelation = new HwBaseUserRelation();
			baseUserRelation.setfUserrelationid(RandomNumberUtil.getNumber());
			baseUserRelation.setfUserid(vo.getfUserid());
			baseUserRelation.setfCategory(1);
			baseUserRelation.setfRoleid(roleid);
			baseUserRelation.setfCreatedate(new Timestamp(System.currentTimeMillis()));
			baseUserRelation.setfCreateuserid(vo.getfCreateuserid());
			baseUserRelation.setfCreateusername(vo.getfCreateusername());
			Integer insert = baseUserRelationMapper.insert(baseUserRelation);
			if (insert != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

	// 查看那些用户绑定了该角色
	public Map<String, Object> queryAllList(BaseUserRelationVo vo) {
		Wrapper<HwBaseUserRelation> wrapper = new EntityWrapper<HwBaseUserRelation>();
		wrapper.eq("F_RoleId", vo.getfRoleid());
		List<HwBaseUserRelation> selectList = baseUserRelationMapper.selectList(wrapper);
		JSONArray array = JSONArray.parseArray(JSON.toJSONString(selectList));
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, array);
	}

	// 移除该角色下的某些用户
	public Map<String, Object> removeUser(List<BaseUserRelationVo> list) {

		// 查看此用户集合是否存在 与该角色已经绑定 的用户
		List<BaseUserRelationVo> insertList = new ArrayList<BaseUserRelationVo>();
		for (BaseUserRelationVo vo : list) {
			Wrapper<HwBaseUserRelation> wrapper = new EntityWrapper<HwBaseUserRelation>();
			wrapper.eq("F_UserId", vo.getfUserid()).and().eq("F_RoleId", vo.getfRoleid());
			List<HwBaseUserRelation> selectList = baseUserRelationMapper.selectList(wrapper);
			// 未绑定关系的保存至集合
			if (!selectList.isEmpty()) {
				insertList.add(vo);
			}
		}

		// 批量删除数据
		for (BaseUserRelationVo vo : insertList) {
			Wrapper<HwBaseUserRelation> wrapper = new EntityWrapper<HwBaseUserRelation>();
			wrapper.eq("F_UserId", vo.getfUserid());
			Integer delete = baseUserRelationMapper.delete(wrapper);
			if (delete != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

	// 查询当前县的流动稽查人员信息
	public Map<String, Object> queryUser(BaseUserRelationVo vo) {
		//查询县稽查人员角色
		HwBaseRole hwBaseRole =new HwBaseRole();
		hwBaseRole.setfFullname("稽查人员");
		HwBaseRole br = hwBaseRoleMapper.selectOne(hwBaseRole);
		//角色用户关系
		Wrapper<HwBaseUserRelation> wrapper = new EntityWrapper<HwBaseUserRelation>();
		wrapper.eq("F_RoleId", br.getfRoleid());
		List<HwBaseUserRelation> hwBaseUserRelationList = baseUserRelationMapper.selectList(wrapper);
		//用户表
		List<HwBaseUser> hwBaseUserList = new ArrayList<HwBaseUser>();
		for(int i=0;i<hwBaseUserRelationList.size();i++){		
			HwBaseUser hBaseUser = new HwBaseUser();
			hBaseUser.setfUserid(hwBaseUserRelationList.get(i).getfUserid());
			hBaseUser.setfAreaid(vo.getfAeraid());
			HwBaseUser hwBaseUser = hwBaseUserMapper.selectOne(hBaseUser);
			if(hwBaseUser!=null){
				hwBaseUserList.add(hwBaseUser);	
			}
			
		}
		JSONArray array = JSONArray.parseArray(JSON.toJSONString(hwBaseUserList));
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, array);
		
	}

}
